;+
; NAME:
;       ADD_COLUMN_BGPS_CSV
;
; PURPOSE:
;       Adds a column to the BGPS Distance Database (IDL structure),
;       includes new header info, but the data cells are empty (0).
;
; CATEGORY:
;       NGPS distance-omnibus Utility
;
; CALLING SEQUENCE:
;       add_column_bgps_csv, bgps_struct, csv_struct, colname,
;       coltype, colunits [,/VERBOSE]
;
; INPUTS:
;       bgps_struct  -- The data from the BGPS CSV file
;       csv_struct   -- The structure containing information about the
;                       CSV file
;       colname      -- Name of the new column to add
;       coltype      -- Data type ('double', 'int', or 'char')
;       colunits     -- Units of the data in the new column
;
; OPTIONAL INPUTS:
;       NONE
;
; KEYWORD PARAMETERS:
;       VERBOSE  -- Be verbose
;
; OUTPUTS:
;       Updates the bgps_struct and csv_struct with the new column information
;
; OPTIONAL OUTPUTS:
;       NONE
;
; EXAMPLE:
;       add_column_bgps_csv, struct, csv, 'junk', 'double', 'MJy/sr'
;
; MODIFICATION HISTORY:
;       Created:  06/03/10, TPEB -- Initial version
;-

PRO ADD_COLUMN_BGPS_CSV, bgps_struct, csv_struct, $
                         new_colname, new_coltype, new_colunits, $
                         VERBOSE=verbose
  
  ;; Define shortcuts
  ncols = csv_struct.ncols
  nrows = csv_struct.nrows
  
  new_colname = strlowcase(new_colname)
  new_coltype = strlowcase(new_coltype)
  
  ;; Set data type for new column
  IF new_coltype EQ 'double' THEN objidp = 0 ELSE $
     IF new_coltype EQ 'int' THEN objidp = 1 ELSE $
        IF new_coltype EQ 'char' THEN objidp = 2 $
        ELSE message,$
     string(new_coltype,new_colname,format=$
            "('Data type ',A0,' for column ',A0,' is invalid!')")
  
  ;; Work with just one element, then replicate
  element = bgps_struct[0]
  
  ;; Add structure element based on CASE argument
  CASE objidp OF
     0 : element = CREATE_STRUCT(element, new_colname, double(0))
     1 : element = CREATE_STRUCT(element, new_colname, long(0))
     2 : element = CREATE_STRUCT(element, new_colname, 'null')
  ENDCASE
  
  ;; Replicate new element into new_struct
  new_struct = REPLICATE(element, csv_struct.nrows)
  
  ;; Copy all elements from previous structure into new structure
  FOR j=0L, ncols-1 DO $
     new_struct.(j) = bgps_struct.(j)
  ;; NOTE: new column is filled with empty cells -- need to run update
  ;;       routine to add the proper data to these cells.
    
  ;; Place new structure into old one
  bgps_struct = TEMPORARY( new_struct )
  
  
  ;; Update CSV header structure
  new_head1 = strarr(ncols+1)
  new_head1[0:ncols-1] = csv_struct.col_name
  new_head1[ncols]     = new_colname 
  
  new_head2 = strarr(ncols+1)
  new_head2[0:ncols-1] = csv_struct.col_type
  new_head2[ncols]     = new_coltype
  
  new_head3 = strarr(ncols+1)
  new_head3[0:ncols-1] = csv_struct.col_units
  new_head3[ncols]     = new_colunits
  
  new_csv = CREATE_STRUCT('nrows', nrows, 'ncols', ncols+1)
  new_csv = CREATE_STRUCT(new_csv, 'col_name', new_head1, $
                          'col_type', new_head2, 'col_units', new_head3)
  
  csv_struct = TEMPORARY( new_csv )
  

 IF KEYWORD_SET( verbose ) THEN $
    message,$
    string(new_colname, new_coltype, format=$
           "('Added new column ',A0,' with data type ',A0)"),/inf
  
END
